import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import layoutHome from '@/layoutHome'
import path from "path";

/**
 * 路由引用组件
 */
// 用户中心
const user = (resolve) => require(['@/views/personCenter/index.vue'], resolve)
/** END */


/**
 * Note: 路由配置项
 *
 * hidden: true                   // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true               // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect           // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'             // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * meta : {
    noCache: true                // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'               // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'             // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false            // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'   // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
  ------------ 新增 noInWhite
  表示是否不在白名单里。true: 不在白名单里

  -------------- 新增 webHidden
  表示是否在  前端导航栏中展示。 true: 不在导航栏中展示
 */


/**
* 个人中心路由
*
* path: 路径
* component: 组件
* redirect: 重定向, 当path内的参数匹配时，url跳转到redirect
* name: 路由名称
* meta: {
*  title: 设置该路由在侧边栏和面包屑中展示的名字
*  icon: 设置该路由的图标，对应路径src/assets/icons/svg *已废弃
*  iconImg: 设置该路由的图标，替代icon，后续会在页面中使用
* }
* hidden: 当设置 true 的时候该路由不会再侧边栏出现 *已废弃
* breadHidden: 当设置 true 的时候该路由不会再侧边栏出现
*/
export const userRouteList = {
  path: 'user',
  name: 'user',
  component: user,
  redirect: '/user/index',
  meta: {
    title: '个人中心',
    webHidden: true,
    noCache: true,
    affix: true,
    icon: 'el-icon-menu',
  },
  breadHidden: true,
  hidden: false,
  children: [
    {
      path: 'index',
      name: 'userIndex',
      meta: {
        title: '控制台',
        webHidden: true,
        noCache: true,
        affix: true,
        icon: 'el-icon-menu',
        iconImg: 'matchPolicy.png'
      },
      hidden: false,
      component: (resolve) => require(['@/views/personCenter/dashboard/index.vue'], resolve),
    },
    {
      path: 'manageAccount',
      name: 'manageAccount',
      meta: {
        title: '账号管理',
        webHidden: true,
        noCache: true,
        affix: true,
        iconImg: 'enterpriseExam.png'
      },
      hidden: false,
      component: (resolve) => require(['@/views/personCenter/manageAccount/index.vue'], resolve),
    },
    {
      path: 'baseInfo',
      name: 'baseInfo',
      meta: {
        title: '企业信息',
        webHidden: true,
        noCache: true,
        affix: true,
        icon: 'el-icon-menu',
        iconImg: 'userInfo.png'
      },
      hidden: false,
      component: (resolve) => require(['@/views/personCenter/baseInfo/index.vue'], resolve),
    },
    {
      path: 'enterprise/form',
      name: 'enterprise',
      component: (resolve) => require(['@/views/personCenter/enterprise/form.vue'], resolve),
      hidden: true,
      meta: {
        title: '我的申报',
        webHidden: true,
        noCache: true,
        affix: true
      },
    },
    {
      name: 'enterprise/declare',
      path: 'declare',
      component: (resolve) => require(['@/views/personCenter/enterprise/declare.vue'], resolve),
      hidden: false,
      meta: {
        title: '我的申报',
        webHidden: true,
        noCache: true,
        affix: true,
        icon: 'el-icon-menu',
        iconImg: 'gazelleEvaluation.png'
      },
    },
    {
      name: 'enterprise/declareList',
      path: 'declareList',
      component: (resolve) => require(['@/views/personCenter/enterprise/declareList.vue'], resolve),
      hidden: true,
      meta: {
        title: '我的申报列表',
        webHidden: true,
        noCache: true,
        affix: true
      },
    },
    {
      path: 'intellectual',
      name: 'intellectual',
      meta: {
        title: '知识产权',
        webHidden: true,
        noCache: true,
        affix: true,
        icon: 'el-icon-menu',
        iconImg: 'innovativeResources.png'
      },
      hidden: false,
      component: (resolve) => require(['@/views/personCenter/intellectual/index.vue'], resolve),
    },
    {
      path: 'intellectual/intellectualEdit',
      name: 'intellectualEdit',
      meta: {
        title: '知识产权编辑',
        webHidden: true,
        noCache: true,
        affix: true,
        noCache: false
      },
      hidden: true,
      component: (resolve) => require(['@/views/personCenter/intellectual/handleInfo.vue'], resolve),
    },

  ]
}

// 存储个人中心相关路由
const tempShowRouteList = []
/**
 * 整理路由 将路由数组中所有元素 { hidden: false } 的path字段拼接完整
 * @param {Arrary} list 路由数组
 * @param {String} basePath 根路径，自行设置
 */
function manageShowRouteList (list, basePath) {
  let menuList = [...list]
  for (let index = 0; index < menuList.length; index++) {
    const element = { ...menuList[index] }

    if (!element.hidden || element.hidden === false) {
      element.path = basePath + '/' + element.path
      tempShowRouteList.push(element)

      if (element.children && element.children.length > 0) {
        manageShowRouteList(element.children, element.path)
      }
    }
  }
}

// 运行
manageShowRouteList(userRouteList.children, '/user')
// 导出
export const userMenuList = tempShowRouteList
/** END */




/** 头部导航路由 */
export const navaRoutes = [
  {
    path: '',
    component: layoutHome,
    redirect: 'index',
    hideen: true,
    children: [
      {
        path: 'index',
        component: (resolve) => require(['@/views/home/index'], resolve),
        name: 'index',
        meta: { title: '首页', noCache: true, affix: true, icon: 'svg-name' }
      },
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/info',
        component: (resolve) => require(['@/views/info/index'], resolve),
        name: 'info',
        meta: { title: '数据', noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/unicorn',
        component: (resolve) => require(['@/views/unicorn/index'], resolve),
        name: 'unicorn',
        meta: { title: '独角兽培育', noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/info/inforank/:rankName',
        component: (resolve) => require(['@/views/info/rank'], resolve),
        name: 'inforank',
        meta: { title: '企业年度数据', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/info/infodetail/:id',
        component: (resolve) => require(['@/views/info/detail'], resolve),
        name: 'infodetail',
        meta: { title: '公司详情', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      },
      {
        path: '/info/schooldetail/:id',
        component: (resolve) => require(['@/views/info/schoolDetail'], resolve),
        name: 'schooldetail',
        meta: { title: '高校详情', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },



  /** 申报 */
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: 'policyDeclare',
        component: (resolve) => require(['@/views/policyDeclare/index'], resolve),
        name: 'policyDeclare',
        meta: { title: '在线申报', noCache: true, affix: true, icon: 'svg-name' },
      },
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/policyDeclare/policyDetail/:id',
        component: (resolve) => require(['@/views/policyDeclare/detail'], resolve),
        name: 'policyDetail',
        meta: { title: '申报详情', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },
  /** END */

  /** 测评 */
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: 'exam',
        component: (resolve) => require(['@/views/exam/index'], resolve),
        name: 'exam',
        meta: { title: '测评', noCache: true, affix: true, icon: 'svg-name' }
      },
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/exam/selectionPage',
        component: (resolve) => require(['@/views/exam/selectionPage'], resolve),
        name: 'selectionPage',
        meta: { title: '测评选择页面', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      },
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/exam/gazelleEvaluation',
        component: (resolve) => require(['@/views/exam/gazelleEvaluation'], resolve),
        name: 'gazelleEvaluation',
        meta: { title: '瞪羚企业测评', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      },
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/exam/unicornEvaluation',
        component: (resolve) => require(['@/views/exam/unicornEvaluation'], resolve),
        name: 'unicornEvaluation',
        meta: { title: '独角兽企业测评', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      },
    ]
  },
  /** END */

  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/level',
        component: (resolve) => require(['@/views/level/index'], resolve),
        name: 'level',
        meta: { title: '榜单', noCache: true, affix: true, icon: 'svg-name' }
      },
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/level/levelrank',
        component: (resolve) => require(['@/views/level/rank'], resolve),
        name: 'levelrank',
        meta: { title: '榜单数据', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/summit',
        component: (resolve) => require(['@/views/summit/index'], resolve),
        name: 'summit',
        meta: { title: '滕王阁峰会', noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      {
        path: '/summit/summitOne',
        component: (resolve) => require(['@/views/summit/summitOne'], resolve),
        name: 'Summitone',
        meta: { title: '首届滕王阁峰会', webHidden: true, noCache: true, affix: true, icon: 'svg-name' }
      }
    ]
  },


  /** 用户中心 */
  {
    path: '',
    component: layoutHome,
    hidden: true,
    children: [
      userRouteList
    ]
  },
  /** END */
]



// 公共路由
export const constantRoutes = [
  // 将其他路由合并
  ...navaRoutes,
  {
    path: '',
    component: layoutHome,
    hidden: true,
    meta: { webHidden: true },
    children: [
      {
        path: 'newsModal',
        component: (resolve) => require(['@/views/newsModal/'], resolve),
        redirect: '/newsModal/notice',
        children: [
          {
            path: '/newsModal/notice',
            name: 'notice',
            component: (resolve) => require(['@/views/notice/index.vue'], resolve),
            hidden: true
          },
          {
            path: '/newsModal/policy',
            name: 'policy',
            component: (resolve) => require(['@/views/policy/index.vue'], resolve),
            hidden: true
          },

        ],
      },
      {
        path: '/newsModal/newspolicyDetail',
        name: 'newspolicyDetail',
        component: (resolve) => require(['@/views/policy/detail'], resolve),
        hidden: true
      },
      {
        path: '/newsModal/noticeDetail',
        name: 'noticeDetail',
        component: (resolve) => require(['@/views/notice/detail.vue'], resolve),
        hidden: true
      }
    ]
  },

  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    name: 'redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'redirectPath',
        component: (resolve) => require(['@/views/redirect'], resolve)
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: (resolve) => require(['@/views/login'], resolve),
    hidden: true
  },
  {
    path: '/forgetPassword',
    name: 'forgetPassword',
    component: (resolve) => require(['@/views/forgetPassword'], resolve),
    hidden: true
  },

  {
    path: '/register',
    name: 'register',
    component: (resolve) => require(['@/views/register.vue'], resolve),
    hidden: true
  },
  // {
  //   path: '/manageInformation',
  //   component: Layout,
  //   name: 'manageInformation',
  //   meta: { title: '企业信息管理', noInWhite: true, noCache: false, affix: false },
  //   children: [
  //     {
  //       name: 'baseInfo',
  //       path: 'baseInfo',
  //       component: (resolve) => require(['@/views/manageInformation/baseInfo/'], resolve),
  //       meta: { title: '企业基本信息', noCache: false, affix: false },
  //     },
  //     {
  //       name: 'intellectual',
  //       path: 'intellectual',
  //       component: (resolve) => require(['@/views/manageInformation/intellectual/index.vue'], resolve),
  //       meta: { title: '知识产权', noCache: false, affix: false },
  //     },
  //     {
  //       name: 'intellectualEdit',
  //       path: 'intellectualEdit',
  //       component: (resolve) => require(['@/views/manageInformation/intellectual/handleInfo.vue'], resolve),
  //       hidden: true,
  //       meta: { title: '知识产权编辑', noCache: false, affix: false },
  //     },
  //   ]
  // },
  // {
  //   path: '/enterprise',
  //   component: Layout,
  //   name: '项目申报管理',
  //   meta: { noInWhite: true },
  //   meta: { title: '项目申报管理', noCache: false, affix: false },
  //   children: [
  //     {
  //       name: 'enterpriseModify',
  //       path: 'modify',
  //       component: (resolve) => require(['@/views/enterprise/modify.vue'], resolve),
  //       meta: { title: '修改企业信息', icon: 'dashboard', noCache: true, affix: true },
  //     },
  //     {
  //       name: 'enterpriseDeclare',
  //       path: 'declare',
  //       component: (resolve) => require(['@/views/enterprise/declare.vue'], resolve),
  //       meta: { title: '独角兽瞪羚企业申报', noCache: true, affix: false },
  //     },
  //     {
  //       name: 'enterpriseForm',
  //       path: 'form',
  //       component: (resolve) => require(['@/views/enterprise/form.vue'], resolve),
  //       hidden: true,
  //       meta: { title: '独角兽瞪羚企业申报表单', noCache: false, affix: false },
  //     },
  //     {
  //       name: 'declareList',
  //       path: 'declareList',
  //       component: (resolve) => require(['@/views/enterprise/declareList.vue'], resolve),
  //       hidden: true,
  //       meta: { title: '独角兽瞪羚企业所有申报表单', noCache: false, affix: false },
  //     }
  //   ]
  // },
  // {
  //   path: '/manageInformation',
  //   component: Layout,
  //   name: '企业信息修改',
  //   meta: { title: '企业信息修改', noCache: false, affix: false },
  //   children: [
  //     {
  //       name: 'baseInfo',
  //       path: 'baseInfo',
  //       component: (resolve) => require(['@/views/manageInformation/baseInfo/'], resolve),
  //       meta: { title: '企业基本信息', noCache: false, affix: true },
  //     }
  //   ]
  // },
  // {
  //   path: '/user',
  //   component: Layout,
  //   meta: { noInWhite: true },
  //   children: [
  //     {
  //       path: 'profile',
  //       component: (resolve) => require(['@/views/system/user/profile/index'], resolve),
  //       name: 'Profile',
  //       hidden: true,
  //       meta: { title: '个人中心', icon: 'user' }
  //     }
  //   ]
  // },
  // {
  //   path: '/system/user-auth',
  //   component: Layout,
  //   hidden: true,
  //   meta: { noInWhite: true },
  //   children: [
  //     {
  //       path: 'role/:userId(\\d+)',
  //       component: (resolve) => require(['@/views/system/user/authRole'], resolve),
  //       name: 'AuthRole',
  //       meta: { title: '分配角色', activeMenu: '/system/user' }
  //     }
  //   ]
  // },
  // {
  //   path: '/system/role-auth',
  //   component: Layout,
  //   hidden: true,
  //   meta: { noInWhite: true },
  //   children: [
  //     {
  //       path: 'user/:roleId(\\d+)',
  //       component: (resolve) => require(['@/views/system/role/authUser'], resolve),
  //       name: 'AuthUser',
  //       meta: { title: '分配用户', activeMenu: '/system/role' }
  //     }
  //   ]
  // },
  // {
  //   path: '/system/dict-data',
  //   component: Layout,
  //   hidden: true,
  //   meta: { noInWhite: true },
  //   children: [
  //     {
  //       path: 'index/:dictId(\\d+)',
  //       component: (resolve) => require(['@/views/system/dict/data'], resolve),
  //       name: 'Data',
  //       meta: { title: '字典数据', activeMenu: '/system/dict' }
  //     }
  //   ]
  // },
  // {
  //   path: '/monitor/job-log',
  //   component: Layout,
  //   hidden: true,
  //   meta: { noInWhite: true },
  //   children: [
  //     {
  //       path: 'index',
  //       component: (resolve) => require(['@/views/monitor/job/log'], resolve),
  //       name: 'JobLog',
  //       meta: { title: '调度日志', activeMenu: '/monitor/job' }
  //     }
  //   ]
  // },
  // {
  //   path: '/tool/gen-edit',
  //   component: Layout,
  //   hidden: true,
  //   meta: { noInWhite: true },
  //   children: [
  //     {
  //       path: 'index/:tableId(\\d+)',
  //       component: (resolve) => require(['@/views/tool/gen/editTable'], resolve),
  //       name: 'GenEdit',
  //       meta: { title: '修改生成配置', activeMenu: '/tool/gen' }
  //     }
  //   ]
  // },

  {
    path: '/404',
    component: (resolve) => require(['@/views/error/404'], resolve),
    hidden: true
  },
  {
    path: '/401',
    component: (resolve) => require(['@/views/error/401'], resolve),
    hidden: true
  },
]



const filterRoutes = (routes, basePath) => {
  let list = []
  routes.forEach(item => {
    if (item.noInWhite) {
      return
    }
    let childrenList = []
    let itemBasePath = path.resolve(basePath, item.path)
    list.push(itemBasePath)
    if (item.children) {
      childrenList = filterRoutes(item.children, itemBasePath)
      list = [...list, ...childrenList]
    }

  })
  return list
}
let allRoutes = filterRoutes(constantRoutes, '')
export const whiteRoutes = Array.from(new Set(allRoutes))

const readyList = (list) => {
  let routes = JSON.parse(JSON.stringify(list))
  let finObj = {}
  for (let i = 0; i < routes.length; i++) {
    const element = routes[i];
    let children = element.children
    if (children) {
      let childrenObj = readyList(children)
      Object.assign(finObj, childrenObj)
    }
    // if (!element.hidden) {
    //   finObj[element.name] = element
    // }
    finObj[element.name] = element
  }
  return finObj
}
export const reallyRouteList = readyList(navaRoutes)


// export const whiteRoutes = filterRoutes(constantRoutes, '')

export default new Router({
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
